package com.sakura.mall.product.attrAttrgroupRelation.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.attrAttrgroupRelation.domain.entity.AttrAttrgroupRelationEntity;
import com.sakura.mall.product.attrAttrgroupRelation.domain.request.AddAttrAttrgroupRelationRequest;
import com.sakura.mall.product.attrAttrgroupRelation.domain.request.UpdateAttrAttrgroupRelationRequest;
import com.sakura.mall.product.attrAttrgroupRelation.domain.request.AttrAttrgroupRelationPageRequest;
import com.sakura.mall.product.attrAttrgroupRelation.domain.vo.AttrAttrgroupRelationVo;
import com.sakura.mall.product.attrAttrgroupRelation.mapper.AttrAttrgroupRelationMapper;
import com.sakura.mall.product.attrAttrgroupRelation.service.AttrAttrgroupRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 属性&属性分组关联 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationMapper, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddAttrAttrgroupRelationRequest request) {
        // todo 其他校验逻辑
        AttrAttrgroupRelationEntity attrAttrgroupRelation = request.convert(AttrAttrgroupRelationEntity.class);
        this.save(attrAttrgroupRelation);
    }

    @Override
    public Page<AttrAttrgroupRelationVo> queryPage(AttrAttrgroupRelationPageRequest request) {
        IPage<AttrAttrgroupRelationEntity> qp = request.toPage();
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<AttrAttrgroupRelationVo> ret = qp.convert(entity -> entity.convert(AttrAttrgroupRelationVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateAttrAttrgroupRelationRequest request) {
        AttrAttrgroupRelationEntity entity = request.convert(AttrAttrgroupRelationEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

